package adsf.adminisadsf.gugedianzishichang.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Process;
import android.view.View;


import adsf.adminisadsf.gugedianzishichang.global.GooglePlayAplaytion;

/**
 * Created by administrator on 17/5/9.
 */

public class UIUtils
{
    public static Context getContext()
    {
        return GooglePlayAplaytion.getContext();
    }

    public static Handler getHandler()
    {
        return GooglePlayAplaytion.getHandler();
    }

    public static int getMainThreadId()
    {
        return GooglePlayAplaytion.getMainThreadId();
    }


    //================加载资源文件==================
    //获取字符串
    public static String getString(int id)
    {
        return getContext().getResources().getString(id);
    }

    //获取字符串数组
    public static String[] getStringArray(int id)
    {
        return getContext().getResources().getStringArray(id);
    }

    //获取图片
    public static Drawable getDrawable(int id)
    {
        return getContext().getResources().getDrawable(id);
    }

    //获取颜色
    public static int getColor(int id)
    {
        return getContext().getResources().getColor(id);
    }

    //根据id获取颜色状态选择器
    public static ColorStateList getColorStateList(int id)
    {
        return getContext().getResources().getColorStateList(id);
    }

    // 获取尺寸
    public static int getDimen(int id) {
        return getContext().getResources().getDimensionPixelSize(id);// 返回具体像素值
    }

    // /////////////////dip和px转换//////////////////////////

    public static int dip2px(float dip) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f);
    }

    public static float px2dip(int px) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return px / density;
    }

    //加载布局文件
    public static View inflate(int id)
    {
        return View.inflate(getContext(),id,null);
    }

    //判断是否允许在住线程
    public static boolean isRunOnUIThread()
    {
        //获取当前线程id ,如果当前线程ID和主线程ID相同,那么就是运行在主线程
        int myTid = Process.myTid();
        if (myTid == getMainThreadId())
        {
            return true;
        }

        return false;
    }

    //运行在主线程
    public static void runOnUIThread(Runnable r)
    {
        if (isRunOnUIThread())
        {
            //主线程,直接运行
            r.run();
        }else
        {
            getHandler().post(r);
        }

    }

}

















